<template>
	<view class="container">
		<!-- <navbar title="商品详情"></navbar> -->
		<view class="wrap" v-if='showPage'>
			<!-- 商品轮播图 和 商品名称 -->
			<view class="goods-info-wrap">
				<view class="swiper-wrap" v-if='swiperList.length > 0'>
				<!-- -->
					<swiper :current='currentIndex' @change='changeImg'  autoplay="true" duration="1000" interval="3500" circular>
						<swiper-item v-for='(item,index) in swiperList'>
							<view class="img">
								<base-img :src='item'></base-img>
							</view>
							<view class="dots-wrap line_center" style="z-index: 999;">{{index + 1}}/{{swiperList.length}}</view>
						</swiper-item>
							
					</swiper>
					
				</view>
				<view class="goods-price-wrap" v-if='goodsType == 1'>
					<view class="left-goods-price">
						<text class="fuhao">￥</text>
						<text class="price">{{goodsDetail.price}}</text>
						<text style="color: #999999;font-size: 32rpx;margin-left: 5rpx;">/{{goodsDetail.unit}}</text>
						
						<!-- <view class="goods-tag">
							<text class="texts">吊牌价</text>
							<text class="money">￥{{goodsDetail.tag_price}}</text>
						</view> -->
					</view>
					
					
					
					
					<view class="share-wrap" @tap='getPoster(goodsDetail.g_id)'>
						<view class="share-icon">
							<image src='../img/share.png'></image>
						</view>
						<!-- 分享 -->
					</view>
				</view>

				<view class="goods-name-wrap">
					{{goodsDetail.goodname}}
			
				</view>
			</view>
			
			<!-- 商品简介 -->
			<view class="goods-detail" >
				<text class="tit">商品详情</text>
				<view class="goods-detai-con">
					<rich-text :nodes="goodsDetail.detail"></rich-text>
				</view>
			</view>
			<view class="footer">
				<view class="footer-wrap" v-if='goodsType == 1'>
					<!-- <view class="add-cart line_center tui-skeleton-rect" @tap="handleGoodsBtn(1)">加入购物车</view> -->
					<view class="buy line_center tui-skeleton-rect" @tap="handleGoodsBtn(2)" style="background: linear-gradient(82deg, #FF7040 0%, #EC1818 100%);" >立即购买</view>
				</view>

			</view>
		</view>
		<!-- <view class="kefu">
			<image src='../img/kefu.png'></image>
		</view> -->
		<!-- 商品购买弹窗 -->
		<modal :showModal='showGoodsModal'>
			<buy-modal :goodsDetail='goodsDetail' :showBuyModal='showGoodsModal' :btnType='btnType'
				@close='close'></buy-modal>
		</modal>
		<!-- 生成海报 -->
		<modal :showModal='showPosterModal'>
			<poster @close='close'></poster>
		</modal>
	</view>
</template>

<script>
	const app = getApp();
	import baseImg from '@/components/base-img/base-img.vue';
	import modal from '@/components/modal/modal.vue';
	import buyModal from '@/components/buy-modal/buy-modal.vue';
	import poster from '@/components/poster/poster.vue';
	export default {
		components: {
			buyModal,
			modal,
			baseImg,
			poster
		},
		data() {
			return {
				showPage: true,
				showGoodsModal: false,
				showPosterModal: false,
				btnType: 0,
				goodsType: 1,
				goodsId: '',
				goodsDetail: {},
				swiperList:[],
				currentIndex: 0
			};
		},
		onLoad(option) {
			if( decodeURIComponent(option.scene) != 'undefined'){
				var arr = []
				arr = decodeURIComponent(option.scene).replace(/_/ig,",").split(',')
				this.goodsId = arr[0]
				console.log(arr)
				getApp().globalData.parent_sn = arr[1]
			}
			if (option.type) this.goodsType = 1;
			if (option.goods_id) this.goodsId = option.goods_id;
			this.getGoodsDetail()
		},
		onHide() {
			this.close()
		},
		methods: {
			/* 获取商品详情数据 */
			getGoodsDetail() {
				let that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				app.globalRequest({
					api: app.getApi().goodsdata.goodsDetail,
					data: {
						g_id: this.goodsId,
						token: uni.getStorageSync('token')
					}
				}).then(res => {
					that.showPage = true;
					if (res.code == 1) {
						let goodsDetail = res.data;
						if(goodsDetail.rotograph){
							that.swiperList = goodsDetail.rotograph.split(',');
						}else{
							that.swiperList = []
						}
						goodsDetail.detail = goodsDetail.detail ? goodsDetail.detail.replace(/\<img/gi, '<img style="max-width:100%"'):'';
						goodsDetail.num = 1;
						that.goodsDetail = res.data;
						console.log('that.goodsDetail', that.goodsDetail)
					}
				}).catch(err => {

				})
			},
			changeImg(e) {
				if (e.detail.source == 'touch') {
					this.currentIndex = e.detail.current;
				}
			},
			getPoster(id) {
				
				let that = this
				// 获取登录状态
				app.globalRequest({
					api: app.getApi().public.islogin,
					method:'post'
				}).then(res => {
					if (res.code == 1) {
						that.login_status = res.data.is_not_login
						if( that.login_status == 1 ){
							uni.showToast({
								title:'请授权登录',
								icon:'none'
							})
							setTimeout(()=>{
								uni.navigateTo({
									url: '/pages/user/login/login_auth',
								})
							},1000)
							return
						}else{
							app.globalRequest({
								api: app.getApi().mall.shareGoods,
								method:'post',
								data: {
									g_id: id
								}
							}).then(res => {
								if (res.code == 1) {
									this.showPosterModal = true;
									uni.$emit('shareUrl',res.data)
								}
							}).catch(err => {
							
							})
						}
					}
				}).catch(err => {
								
				})
			
				
			},
			handleGoodsBtn(type) {
				this.btnType = type;
				this.showGoodsModal = true;
			},
			clickEv() {
				console.log('1111111111111')
				this.showGoodsModal = true;
			},
			close() {
				this.showGoodsModal = false;
				this.showPosterModal = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	image {
		width: 100%;
		height: 100%;
	}
	.wrap{
		
	}

	.goods-info-wrap {
		background: $dis-con-color;
		padding: 0 0rpx 32rpx;
	}

	.rich-img {
		max-width: 100% !important;
		width: 100% !important;
		height: auto !important;
		display: block
	}
	.goods-tag{
		// margin: 40rpx 20rpx;
		// border: 2rpx solid #EC1818;
		width: 40%;
		font-size: 24rpx;
		display: flex;
		align-items: center;
		margin-left: 20rpx;
		.texts{
			border-radius: 4rpx 0rpx 0rpx 4rpx;
			padding: 7rpx 10rpx;
			box-sizing: border-box;
			color: #fff;
			background-color: #EC1818;
		}
		.money{
			display: flex;
			justify-content: center;
			width: 55%;
			border: 2rpx solid #EC1818;
			color: #EC1818;
			padding: 5rpx 10rpx;
			box-sizing: border-box;
			border-radius: 0rpx 4rpx 4rpx 0rpx;
		}
	}
	.swiper-wrap {
		width: 100%;
		height: 750rpx;
		position: relative;

		swiper {
			width: 100%;
			height: 100%;

			swiper-item,
			.img {
				width: 100%;
				height: 100%;
			}
		}

		.dots-wrap {
			width: 84rpx;
			height: 40rpx;
			background: rgba(255, 255, 255, 0.67);
			border-radius: 39rpx;
			position: absolute;
			right: 24rpx;
			bottom: 24rpx;
			font-size: 20rpx;
		}
	}

	.goods-inte-wrap {
		margin-top: 16rpx;
		display: flex;
		align-items: baseline;
		font-size: 50rpx;
		color: $dis-inter-color;

		.inte-icon {
			width: 28rpx;
			height: 28rpx;
			margin-right: 16rpx;
		}
	}

	.goods-price-wrap {
		margin-top: 32rpx;
		display: flex;
		justify-content: space-between;
		padding: 0 32rpx;
		box-sizing: border-box;
		
		.left-goods-price {
			display: flex;
			align-items: center;
			color: $dis-font-color;
			width: 85%;
			.fuhao {
				font-size: 34rpx;
			}

			.price {
				font-size: 50rpx;
			}
		}

		.goods-vip-price-wrap {
			display: flex;
			margin-left: 10px;

			.vip-icon {
				width: 42rpx;
				height: 36rpx;
				position: relative;
				z-index: 2;
				flex-shrink: 0;
			}

			.vip-price {
				padding: 0 15rpx;
				height: 30rpx;
				background: linear-gradient(90deg, #FEB147 0%, #FEEABB 100%);
				border-radius: 0rpx 14rpx 14rpx 0rpx;
				font-size: 20rpx;
				margin-left: -20rpx;
				position: relative;
				z-index: 1;
				top: 2rpx;

				.vip-price-text {
					margin-left: 8rpx;
					color: #964301;
					line-height: 30rpx;
				}
			}
		}

		.share-wrap {
			display: flex;
			align-items: center;
			font-size: 24rpx;

			.share-icon {
				width: 28rpx;
				height: 28rpx;
				margin-right: 14rpx;
			}
		}
	}

	.goods-name-wrap {
		margin-top: 8rpx;
		padding: 0 32rpx;
		box-sizing: border-box;
		color: #333333;
		font-size: 32rpx;
	}

	.goods-detail {
		margin-top: 20rpx;
		padding: 32rpx 32rpx 200rpx;
		background: $dis-con-color;
		.tit{
			font-size: 32rpx;
			color: #313131;
		}
		.goods-detai-con {
			min-height: 600rpx;
			margin-top: 40rpx;
		}
	}

	.footer {
		width: 100%;
		height: 150rpx;
		background: #fff;
		padding: 0 30rpx;
		box-sizing: border-box;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 99;
		display: flex;
		align-items: center;
		justify-content: center;

		.footer-wrap {
			width: 90%;
			display: flex;
			justify-content: center;
			// padding: 0 24rpx;
			box-sizing: border-box;
			color: #fff;
		}

		.add-cart {
			width: 224rpx;
			height: 90rpx;
			background: #F7CF02;
		}

		.buy {
			flex: 1;
			height: 90rpx;
			background: $dis-font-color;
		}

		.exchange-btn-wrap {
			width: 506rpx;
			height: 80rpx;
			background: $dis-font-color;
			font-size: 30rpx;
			color: #fff
		}
	}

	.kefu {
		width: 86rpx;
		height: 86rpx;
		position: fixed;
		right: 32rpx;
		top: 1000rpx;
		z-index: 222;
	}
</style>